import {getParams, IParams} from '../src/params';
import * as process from 'process';
import {expect} from 'chai'

describe('getParams', () => {
    beforeEach(() => {
        // Mocking environment variables before each test
        process.env.PIPELINE_ID = '123';
        process.env.PIPELINE_NAME = 'test-pipeline';
        process.env.BUILD_NUMBER = '456';
        process.env.WORK_SPACE = '/test/workspace';
        process.env.PROJECT_DIR = '/test/project';
        process.env.BUILD_JOB_ID = '789';
        process.env.JENKINS_MASTER = 'http://master.example.com';
        process.env.JENKINS_USERNAME = 'user';
        process.env.JENKINS_PASSWORD = 'pass';
        process.env.JENKINS_JOB = 'test-job';
        process.env.JENKINS_BUILD_NUMBER = '12';
        process.env.PARAMS = 'test-params';
    });

    it('should return correct parameters', () => {
        const expectedParams: IParams = {
            pipelineID: 123,
            pipelineName: 'test-pipeline',
            buildNumber: 456,
            workSpace: '/test/workspace',
            projectDir: '/test/project',
            buildJobID: 789,
            jenkinsMaster: 'http://master.example.com',
            jenkinsUsername: 'user',
            jenkinsPassword: 'pass',
            jenkinsJob: 'test-job',
            jenkinsBuildNumber: 12,
            params: 'test-params'
        };

        const params = getParams();
        expect(params).to.deep.equal(expectedParams)
    });

    it('should return correct parameters', () => {
        process.env.JENKINS_JOB_URL = 'http://master.example.com/job/test-job-dir/job/test-job-by-url';

        const expectedParams: IParams = {
            pipelineID: 123,
            pipelineName: 'test-pipeline',
            buildNumber: 456,
            workSpace: '/test/workspace',
            projectDir: '/test/project',
            buildJobID: 789,
            jenkinsMaster: 'http://master.example.com',
            jenkinsUsername: 'user',
            jenkinsPassword: 'pass',
            jenkinsJob: 'test-job-dir/job/test-job-by-url',
            jenkinsBuildNumber: 12,
            params: 'test-params'
        };

        const params = getParams();
        expect(params).to.deep.equal(expectedParams)

        delete process.env.JENKINS_JOB_URL
    });
});
